package com.br.negocio.dao;


import java.util.Collections;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.br.negocio.vo.ServicoVendaVO;
import com.br.negocio.vo.VendaVO;

@Service("vendaDAO")
public class VendaDAO {

        @PersistenceContext
        private EntityManager entityManager;

        
        public void inserirVenda(VendaVO vendaVO) {
                try {
                        entityManager.persist(vendaVO);
                        
                        for ( ServicoVendaVO ServicoVenda :  vendaVO.getListaServicoVendaVO() ){
                                ServicoVenda.setVendaVO(vendaVO);
                                ServicoVenda.getServicoVendaPK().setCodVenda(vendaVO.getCodigo());
                                ServicoVenda.getServicoVendaPK().setCodservico(ServicoVenda.getServicoVO().getCodigo());
                                entityManager.persist(ServicoVenda);
                        }
                        
                } catch (Exception e) {
                        throw new RuntimeException("Erro ao inserir venda: " + e.getMessage(), e);
                }
        }
        
        @Transactional(propagation = Propagation.REQUIRED)
        public void atualizarVenda(VendaVO vendaVO) {
                try {
                        entityManager.merge(vendaVO);
                } catch (Exception e) {
                        throw new RuntimeException("Erro ao atualizar venda: " + e.getMessage(), e);
                }
        }

        @Transactional(propagation = Propagation.REQUIRED)
        public void removerVenda(VendaVO vendaVO) {
                try {
                        if (!entityManager.contains(vendaVO)) {
                                vendaVO = entityManager.merge(vendaVO);
                        }
                        entityManager.remove(vendaVO);
                } catch (Exception e) {
                        throw new RuntimeException("Erro ao remover venda: " + e.getMessage(), e);
                }
        }

        @SuppressWarnings("unchecked")
        @Transactional(propagation = Propagation.REQUIRED)
        public VendaVO obterVenda(VendaVO vendaVO) {
                try {
                        VendaVO vendaAux = new VendaVO();
                        vendaAux = entityManager.find(VendaVO.class, vendaVO.getCodigo());
                        /*
                        List<ServicoCompraVO> listaPc = new ArrayList<ServicoCompraVO>();
                        listaPc.addAll((List)compraAux.getListaServicoCompraVO());
                        compraAux.setListaServicoCompraVO(new ArrayList<ServicoCompraVO>());
                        compraAux.setListaServicoCompraVO(listaPc);
                        */
                        Query query = entityManager.createNamedQuery("loadAllServicoVenda");
                        query.setParameter("codigo", vendaAux.getCodigo());
                        
//                      //List<ServicoCompraVO> lista = (List<ServicoCompraVO>) entityManager.createNamedQuery("loadAllServicoCompra").getResultList();
                        List<ServicoVendaVO> lista = (List<ServicoVendaVO>) query.getResultList();
                        if ((lista != null) && (!lista.isEmpty())) {
                                vendaAux.setListaServicoVendaVO(lista);
                        } else {
                                vendaAux.setListaServicoVendaVO(Collections.EMPTY_LIST);
                        }
                        
                        
                        return vendaAux;
                        
                } catch (Exception e) {
                        throw new RuntimeException("Erro ao buscar venda: " + e.getMessage(), e);
                }
        }

        @SuppressWarnings("unchecked")
        @Transactional(propagation = Propagation.REQUIRED)
        public List<VendaVO> obterListaVenda() {
                try {
                        List<VendaVO> lista = (List<VendaVO>) entityManager.createNamedQuery("loadAllVenda").getResultList();
                        if ((lista != null) && (!lista.isEmpty())) {
                                return lista;
                        } else {
                                return Collections.EMPTY_LIST;
                        }
                } catch (Exception e) {
                        throw new RuntimeException("Erro ao carregar todas as vendas.", e);
                }
        }
        
        @SuppressWarnings("unchecked")
        @Transactional(propagation = Propagation.REQUIRED)
        public boolean validaDuplicado(VendaVO vendaVO) {
                Query query = entityManager.createNamedQuery("findDuplicatedVenda");
                query.setParameter("codigo", vendaVO.getCodigo());
                query.setParameter("nota", vendaVO.getNota());
                query.setParameter("notaSerie", vendaVO.getNotaSerie());
                List lista = query.getResultList();
                if (lista.isEmpty()) {
                        return true;
                } else {
                        return false;
                }
        }

        
        
        public void setEntityManager(EntityManager entityManager) {
                this.entityManager = entityManager;
        }

}
